<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get_detail';
if($_RQ['op']=='get_detail'){
	$shopset = plugin('jy_weishop')->plugin_setting('shopset');
	set_html('title','商品详情');
	$id=intval($_RQ['id']);
	//来自其他插件的购物
	$this->session('fromplugin_name',$_RQ['plugin_name']);
	$this->session('frompid',intval($_RQ['frompid']));
	if($id){
		$_SESSION['oid'] = $id;
	}
	if(!$id){
		exi('没有商品ID','error','index/');
	}
	$item=pdo_get('jy_weishop_video',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if(!$item){
		exi('选择的视频不存在，请重新选择！','error','index/');
	}
	$goods=model('jy_weishop_en/goods')->get_goodsitem($item['goodsid']);
	$goods['image']=tomedia($goods['image']);
	//更新足迹
	$history=pdo_getcolumn('jy_weishop_video_looklist',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'oid'=>$id),'id');
	if($history){
		pdo_update('jy_weishop_video_looklist',array('oid'=>$id,'createtime'=>time()),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'oid'=>$id));
	}else{
		pdo_insert('jy_weishop_video_looklist',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'oid'=>$id,'createtime'=>time()));
	}
	//判断是否收藏：
	$item['islike']=pdo_getcolumn('jy_weishop_video_like',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'oid'=>$id),'id');
	$item['islike']=$item['islike']>0?1:0;
	//判断买家秀是否开启
	//营销设置
	$saleset=plugin('jy_weishop')->plugin_setting('saleset');
	$member=pdo_get('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	$item['canbuy'] = ($item['status'] == 1) && empty($item['deleted']) && ($item['total'] > 0);
	$item['unit'] = ((empty($item['unit']) ? '件' : $item['unit']));
	$item['images'] = json_decode($item['images'],true);
	foreach($item['images'] as &$image){
		$image=tomedia($image);
	}
	$item['images']=$images[0]?$images:array($item['image']);
	$item['image']=tomedia($item['image']);
	$item['video']=tomedia($item['video']);
	$item['subtitle'] = cfc('tools')->substr($item['subtitle'],0,48);
	//小程序二维码：
	$url='/jy_weishop_en/video/detail/index?id='.$id.'&fromuser='.$_SESSION['uid'];
	$pid='id'.$id;
	$item['appcode'] = model('jy_weishop_en/model')->get_wxappewm($url,$pid);
	$item['subtitle1']=cfc('tools')->substr($item['subtitle1'],0,13);
	$item['title1']=cfc('tools')->substr($item['title'],0,14);
	$item['title2']=cfc('tools')->substr($item['title'],14,13);
	$item['title2']=$item['subtitle1']?$item['subtitle1']:$item['title2'];
	$item['title2']=$item['title2']?$item['title2'].'...':'暂无描述';
	$item['looks']=pdo_count('jy_weishop_video_looklist',array('uniacid'=>$_SESSION['uniacid'],'oid'=>$item['id']),'id');
	$item['likes']=pdo_count('jy_weishop_video_like',array('uniacid'=>$_SESSION['uniacid'],'oid'=>$item['id']),'id');
	$pindex = max(1, 0);
	$psize = 5;
	$members=pdo_getslice('jy_weishop_video_looklist',array('uniacid'=>$_SESSION['uniacid'],'oid'=>$item['id']),array($pindex,$psize),$total,array('mid'),'','id desc');
	foreach($members as &$mv){
				$memberinfo=pdo_get('core_members',array('id'=>$mv['mid']),array('nickname','avatar'));
				$mv['nickname']=$memberinfo['nickname'];
				$mv['avatar']=tomedia($memberinfo['avatar']);
	}
	$return['lookusers']=$members;
	$psize = 50;
	$msgs=pdo_getslice('core_account_message',array('uniacid'=>$_SESSION['uniacid'],'aid'=>$item['id'],'plugin_name'=>'jy_weishop'),array($pindex,$psize),$total,'','','id desc');
	foreach($msgs as &$m){
		$m['avatar']=pdo_getcolumn('core_members',array('id'=>$m['uid']),'avatar');
	}
	$return['msgs']=$msgs;
	$psize = 1;
	$next=pdo_getslice('jy_weishop_video',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'id >'=>$item['id']),array($pindex,$psize),$total,'','','id asc');
	$return['nextid']=$next[0]['id'];
	if(!$return['nextid']){
		$next=pdo_getslice('jy_weishop_video',array('uniacid'=>$_SESSION['uniacid'],'status'=>1),array($pindex,$psize),$total,'','','id asc');
		$return['nextid']=$next[0]['id'];
	}
	$return['nexturl']=custom_url('jy_weishop_en/video.detail//id='.$return['nextid'],true);
	$on=pdo_getslice('jy_weishop_video',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'id <'=>$item['id']),array($pindex,$psize),$total,'','','id desc');
	$return['onid']=$on[0]['id'];
	if(!$return['onid']){
		$on=pdo_getslice('jy_weishop_video',array('uniacid'=>$_SESSION['uniacid'],'status'=>1),array($pindex,$psize),$total,'','','id desc');
		$return['onid']=$on[0]['id'];
	}
	$return['onurl']=custom_url('jy_weishop_en/video.detail//id='.$return['onid'],true);
	$return['wsConfig'] =array('isIos' => is_ios(),'isMobile'=>1);
	//POST提交：
	$post = array();
	$post['num'] = 1;
	$post['goodsid'] = $item['goodsid'];
	$post['specid']=$selectspecs;
	$post['addressid']=$address['id']?:0;
	$post['optionsid']=$return['options']['id']?:0;
	$post['storeid']=0;
	$return['post']=$post;
	$return['item']=$item;
	$return['specs_list']=$goods['specs_list'];
	$return['goods']=$goods;
	//现在时间时间戳
	$return['sharedata']=array(
		'title'=>$item['title'],
		'desc'=>$item['subtitle']?$item['subtitle']:$item['title'],
		'link'=>custom_url('jy_weishop_en/video.detail//id='.$id.'&fromuser='.$_SESSION['uid'],true),
		'imgUrl'=>ATTACHMENT_URL.$item['image']
	);
	$fans=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','mobile','avatar','nickname'));
	$fans['time']=date('Y-m-d H:i:s',TIMESTAMP);
	$fans['nickname']=$fans['nickname']?$fans['nickname']:'匿名';
	$return['member']=array_merge($fans,$return['member']);
	$return['item']['price']=price($return['item']['price']);
	$return['is_nolive']=true;
	exi($return);
}

if($_RQ['op']=='addtocart'){
	$post=get_postdata();
	$id=intval($post['goodsid']);
	$number=intval($post['num']);
	if(!$id){
		exi('选择的商品不存在，请重新选择！','error');
	}
	if(!$number){
		$number=1;
	}
	$spsids = '';
	if($post['specid']){
		foreach($post['specid'] as $k=>&$s){
			if($k==0){
				$spsids .= $s;
			}else{
				$spsids .= '_'.$s;
			}
		}
	}
	$optionid = pdo_getcolumn('jy_weishop_goods_option',array('goodsid'=>$id,'specs'=>$spsids),'id');
	$item=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$ifgoods=pdo_get('jy_weishop_member_cart',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'goodsid'=>$id,'optionid'=>$optionid));
	$data=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'goodsid'=>$id,
		'price'=>$item['price'],
		'optionid'=>$optionid,
	);
	if($ifgoods){
		$data['total']=$ifgoods['total']+$number;
		if (0 < $item['usermaxbuy']) {
			$order_goodscount = pdo_fetchcolumn('select ifnull(sum(og.total),0)  from ' . tablename('jy_weishop_order_goods') . ' og ' . ' left join ' . tablename('jy_weishop_order') . ' o on og.orderid=o.id ' . ' where og.goodsid=:goodsid and  o.status>=1 and o.mid=:mid  and og.uniacid=:uniacid ', array(':goodsid' => $item['id'], ':uniacid' => $_SESSION['uniacid'], ':mid' => $_SESSION['uid']));
			if ($item['usermaxbuy'] <= $order_goodscount +$ifgoods['total']) {
				exi('不能再加了，超过该商品最大购买量！','error');
			}
		}
		if (0 < $item['maxbuy']) {
			if ($item['maxbuy'] <= $data['total']) {
				exi('不能再加了，超过该商品单次购买量！','error');
			}
		}
		pdo_update('jy_weishop_member_cart',$data,array('uniacid'=>$_SESSION['uniacid'],'id'=>$ifgoods['id']));
	}
	else{
		$data['total']=$number;
		//如果来自其他商户分销（保存商户ID）
		$data['total']['merchid']=$this->session('frommerchid');
		pdo_insert('jy_weishop_member_cart',$data);
	}
	exi('成功加入购物车！');
}
if($_RQ['op']=='get_optionsid'){
	$post=get_postdata();
	$id=$post['goodsid']?:$_SESSION['goodsid'];
	if(!$id){
		exi('请先选择商品！','error');
	}
	//规格
	$specids=explode('_',$post['specid']);
	
	//已选规格
	$selespename = '';
	if($specids){
			foreach($specids as $k=>$sp){
				$spec_item=pdo_get('jy_weishop_goods_spec_item',array('id'=>$sp,'uniacid'=>$_SESSION['uniacid']),array('id','title'));
				$selespename .=$spec_item['title'].'，';
			}
	}
	$return['options'] = pdo_get('jy_weishop_goods_option',array('goodsid'=>$id,'specs'=>$post['specid']));
	//默认商品规格价格：
	if($return['options']){
		$item['price']=$return['options']['price'];
	}
	$return['carttotal']=pdo_getcolumn('jy_weishop_member_cart',array('optionid'=>$return['options']['id'],'goodsid'=>$id,'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
	$return['carttotal']=$return['carttotal']?:0;
	$return['selespename'] = $selespename;
	exi($return);
}
if($_RQ['op']=='change_address'){
	$post=get_postdata();
	$addressid = intval($post['addressid']);
	$id=$_SESSION['goodsid'];
	if(!$id){
		exi('请先选择商品！','error');
	}
	$address =  pdo_get('core_address',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'id'=>$addressid));
	$address['pcd']=json_decode($address['pcd'],true);
	$return['address'] =$address;
	exi($return);
}
if($_RQ['op']=='setmsg'){
	$member=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('credit1','credit2','mobile','groupid','avatar','nickname'));
	$postdata=get_postdata();
	$data=array(
		'aid'=>intval($postdata['id']),
		'content'=>trim($postdata['msg']),
		'username'=>$member['nickname'],
		'uid'=>$_SESSION['uid'],
		'uniacid'=>$_SESSION['uniacid'],
		'plugin_name'=>'jy_weishop',
		'createtime'=>time(),
	);
	pdo_insert('core_account_message',$data);
	$return=array('msg'=>'评论成功！','avatar'=>$member['avatar']);
	exi($return);
}
if($_RQ['op']=='like'){
	$id = $_SESSION['oid'];
	if(!$id){
		exi('没有找到该商品，请重新选择！','error');
	}
	$like=pdo_get('jy_weishop_video_like',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'oid'=>$id),'id');
	if($like){
			pdo_delete('jy_weishop_video_like',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'oid'=>$id));
			exi('取消成功');
	}else{
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'oid'=>$id,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_weishop_video_like',$insert);
		exi('点赞成功');
	}	
}

if($_RQ['op']=='check_buy'){
	model('jy_weishop_en/goods')->check_buy($_SESSION['goodsid'],$_RQ['num'],$_RQ['optionsid']);
	exi('','','jy_weishop_en/order.create//goodsid='.$_SESSION['goodsid'].'&num='.$_RQ['num'].'&optionsid='.$_RQ['optionsid'].'&addressid='.$_RQ['addressid'].'&storeid='.$_RQ['storeid']);
}
if($_RQ['op']=='getcomments'){
	$itemid=$_RQ['goodsid']?intval($_RQ['goodsid']):$_SESSION['goodsid'];
	$return['list']=model('jy_weishop_en/goods')->get_comments($itemid,6,$comment_total,$_RQ['type'],$_RQ['page']);
	exi($return);
}